/**
 * 编辑定时任务详情对话框
 */
var TimetaskInfoDlg = {
    data: {
		taskId: "",
		name: "",
		groupName: "",
		startTime: "",
		endTime: "",
		cron: "",
		jobStatus: "",
		planStatus: "",
		concurrent: "",
		jobData: "",
		methodName: "",
		beanName: "",
		description: "",
		createUser: "",
		updateUser: "",
		createTime: "",
		updateTime: ""
    },
    tableId:"cronTable"

};

layui.use(['layer', 'form', 'table','admin', 'ax'], function () {
    var $ = layui.jquery;
    var $ax = layui.ax;
    var form = layui.form;
    var table = layui.table;
    var admin = layui.admin;
    var layer = layui.layer;

    /**
     * 开启提示框
     */
    Feng.tips({tips: 1,time:0})

    /**
     * 初始化表格的列
     */
    initColumn = function () {
        return [[
            {type: 'numbers'},
            {field: 'cron', sort: false, title: '近7次执行时间'}
        ]];
    };

    /**
     * 表单验证
     */
    form.verify({
        namelength: function (value) {
            var i,sum;
            sum=0;
            for(i=0;i<value.length;i++){
                if ((value.charCodeAt(i)>=0) && (value.charCodeAt(i)<=255))
                    sum=sum+1;
                else
                    sum=sum+2;
            }
            if (sum > 80) {
                return '最多只能是80个字符';
            }
        },
        checkname: function (value) {
            var check = false;
            var ajax = new $ax(Feng.ctxPath + "/timetask/checkName", function (data) {
               check = data.data;
            });
            var taskId =$("input[name='taskId']").val()
            var name =$("input[name='name']").val()
            var groupName =$("input[name='groupName']").val()
            if(taskId !=null && taskId!=""){
                ajax.set("taskId", taskId);
            }
            ajax.set("name", name);
            ajax.set("groupName", groupName);
            ajax.start();
            if(check){
                return '任务组+名已经存在，请修改';
            }
        },
        grouplength: function (value) {
            var i,sum;
            sum=0;
            for(i=0;i<value.length;i++){
                if ((value.charCodeAt(i)>=0) && (value.charCodeAt(i)<=255))
                    sum=sum+1;
                else
                    sum=sum+2;
            }
            if (sum > 20) {
                return '最多只能是20个字符';
            }
        },
        cronrule: function (value) {
            var check = false;
            var ajax = new $ax(Feng.ctxPath + "/timetask/checkCron", function (data) {
                check = data.data
            });
            ajax.set("cron", value);
            ajax.start();
            if(!check){
                return '表达式格式错误';
            }
        },

        descriptionlength: function (value) {
            var i,sum;
            sum=0;
            for(i=0;i<value.length;i++){
                if ((value.charCodeAt(i)>=0) && (value.charCodeAt(i)<=255))
                    sum=sum+1;
                else
                    sum=sum+2;
            }
            if (sum > 500) {
                return '任务描述最多只能500个字符或者250个中文字';
            }
        }

    });

    // 让当前iframe弹层高度适应
    admin.iframeAuto();
	
	//获取详情信息，填充表单
    var ajax = new $ax(Feng.ctxPath + "/timetask/detail?taskId=" + Feng.getUrlParam("taskId"));
    var result = ajax.start();
    form.val('timetaskForm', result.data);

    // 表单提交事件
    form.on('submit(btnSubmit)', function (data) {
        var ajax = new $ax(Feng.ctxPath + "/timetask/edit", function (data) {
            Feng.success("添加成功！");
            //返回
			window.location.href = Feng.ctxPath + "/timetask?taskId=" + $("#taskId").val();
            /** 窗口返回前页模式
            //传给上个页面，刷新table用
            //admin.putTempData('formOk', true);

            //关掉对话框
            admin.closeThisDialog();
            */
        }, function (data) {
            Feng.error("添加失败！" + data.responseJSON.message)
        });
        ajax.set(data.field);
        ajax.start();
		return false;//返回
    });
	
	//返回按钮，需要同步修改页面按钮id
    $("#backupPage").click(function () {
        window.location.href = Feng.ctxPath + "/timetask?taskId=" + $("#taskId").val();
    });

    // 渲染表格
    var queryData = {};
    queryData['cron'] =$("input[name='cron']").val();
    var tableResult = table.render({
        elem: '#'+TimetaskInfoDlg.tableId,
        height: 309,
        url: Feng.ctxPath + '/timetask/analysisCron',
        page: false,
        cellMinWidth: 100,
        cols: initColumn(),
        where:queryData
    });

    //cron执行按钮
    $("#analysisCron").click(function () {
        var queryData = {};
        queryData['cron'] =$("input[name='cron']").val();
        table.reload(TimetaskInfoDlg.tableId, {where: queryData});
        return false;
    });

});